package com.qidianit.mes.pro.controller;

import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.ProcessStep;
import com.qidianit.mes.pro.service.ProcessStepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Description:
 * @Author: shanzhe
 * @Date: 2025/9/17 - 16:27
 */
@RestController
@RequestMapping("/processStep")
@CrossOrigin
public class ProcessStepController {

    @Autowired
    private ProcessStepService processStepService;

    @GetMapping("/page")
    public Result<PageInfo<ProcessStep>>  queryPagedProcessStepsByProcessCode(@RequestParam(value = "processId",required = true) Integer processId,
                                                                              @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                                              @RequestParam(value = "size",defaultValue = "10") Integer size)
    {
        return Result.success(processStepService.queryPagedProcessStepsByProcessCode(processId,pageNum,size));
    }

    @GetMapping("/get/{id}")
    public Result queryProcessStepById(@PathVariable("id") Integer id) {
        return Result.success(processStepService.queryProcessStepById(id));
    }

    @PostMapping("/add")
    public Result addProcessStep(@RequestBody ProcessStep processStep) {
        processStep.setCreateTime(LocalDateTime.now());
        processStep.setCreatorId(1001);
        int result = processStepService.addProcessStep(processStep);
        return result == 1?Result.success(processStep):Result.error("操作失败");
    }

    @PutMapping("/update")
    public Result updateProcessStep(@RequestBody ProcessStep processStep) {
        processStep.setUpdateTime(LocalDateTime.now());
        processStep.setModifierId(1002);
        int result = processStepService.updateProcessStep(processStep);
        return result == 1?Result.success():Result.error("操作失败");
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteProcessStepById(@PathVariable("id") Integer id) {
        int result = processStepService.deleteProcessStepById(id);
        return result == 1?Result.success():Result.error("操作失败");
    }
}
